package org.jeecg.modules.consumptionSteam.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.chargeSteam.service.IChargeSteamService;
import org.jeecg.modules.consumptionSteam.entity.ConsumptionSteam;
import org.jeecg.modules.consumptionSteam.entity.SteamSelectDTO;
import org.jeecg.modules.consumptionSteam.service.IConsumptionSteamService;
import org.jeecg.modules.feeSteam.entity.FeeSteam;
import org.jeecg.modules.feeSteam.service.IFeeSteamService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.MathContext;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description: 水蒸汽能耗
 * @Author: jeecg-boot
 * @Date:   2023-06-20
 * @Version: V1.0
 */
@Api(tags="水蒸汽能耗")
@RestController
@RequestMapping("/consumptionSteam/consumptionSteam")
@Slf4j
public class ConsumptionSteamController extends JeecgController<ConsumptionSteam, IConsumptionSteamService> {
	@Autowired
	private IConsumptionSteamService consumptionSteamService;
	@Autowired
	private IChargeSteamService chargeSteamService;
	@Autowired
	private IFeeSteamService feeSteamService;
	
	/**
	 * 分页列表查询
	 *
	 * @param consumptionSteam
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "水蒸汽能耗-分页列表查询")
	@ApiOperation(value="水蒸汽能耗-分页列表查询", notes="水蒸汽能耗-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ConsumptionSteam consumptionSteam,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ConsumptionSteam> queryWrapper = QueryGenerator.initQueryWrapper(consumptionSteam, req.getParameterMap());
		Page<ConsumptionSteam> page = new Page<ConsumptionSteam>(pageNo, pageSize);
		IPage<ConsumptionSteam> pageList = consumptionSteamService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @PostMapping("/select")
	 public Result<?> select(@RequestBody SteamSelectDTO selectDTO){
		 List<ConsumptionSteam> list = consumptionSteamService.selectByTimeAndDept(selectDTO);
		 return Result.OK(list);
	 }

	@GetMapping("/getAllGroupByDept")
	public Result<?> getAllGroupByDept(){
		List<Map<String, Object>> list = consumptionSteamService.getAllGroupByDept();
		return Result.OK(list);
	}
	
	/**
	 *   添加
	 *
	 * @param consumptionSteam
	 * @return
	 */
	@AutoLog(value = "水蒸汽能耗-添加")
	@ApiOperation(value="水蒸汽能耗-添加", notes="水蒸汽能耗-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ConsumptionSteam consumptionSteam) {
		setDate(consumptionSteam);
		FeeSteam feeSteam = getFeeSteam(consumptionSteam);
		feeSteamService.save(feeSteam);
		consumptionSteamService.save(consumptionSteam);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param consumptionSteam
	 * @return
	 */
	@AutoLog(value = "水蒸汽能耗-编辑")
	@ApiOperation(value="水蒸汽能耗-编辑", notes="水蒸汽能耗-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ConsumptionSteam consumptionSteam) {
		setDate(consumptionSteam);
		FeeSteam feeSteam = getFeeSteam(consumptionSteam);
		feeSteamService.updateById(feeSteam);
		consumptionSteamService.updateById(consumptionSteam);
		return Result.OK("编辑成功!");
	}

	private static void setDate(ConsumptionSteam consumptionSteam) {
		if(StringUtils.isNotEmpty(consumptionSteam.getMonth())&&StringUtils.isNotEmpty(consumptionSteam.getYear())){
			String month=String.format("%2s",consumptionSteam.getMonth()).replace(" ", "0");
			String date= consumptionSteam.getYear()+month;
			consumptionSteam.setDate(Integer.parseInt(date));
		}
	}

	@NotNull
	private FeeSteam getFeeSteam(ConsumptionSteam consumptionSteam) {
		FeeSteam feeSteam = new FeeSteam();
		BigDecimal consumption = new BigDecimal(consumptionSteam.getConsumption());
		Map<String, Object> map = chargeSteamService.getProgramme();
		BigDecimal price = (BigDecimal) map.get("price");
		BigDecimal multiply = consumption.multiply(price, new MathContext(2));
		feeSteam.setId(consumptionSteam.getId()).setFee(multiply).setDept(consumptionSteam.getDept()).setYear(consumptionSteam.getYear())
				.setMonth(consumptionSteam.getMonth()).setDate(consumptionSteam.getDate());
		return feeSteam;
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "水蒸汽能耗-通过id删除")
	@ApiOperation(value="水蒸汽能耗-通过id删除", notes="水蒸汽能耗-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		consumptionSteamService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "水蒸汽能耗-批量删除")
	@ApiOperation(value="水蒸汽能耗-批量删除", notes="水蒸汽能耗-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.consumptionSteamService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "水蒸汽能耗-通过id查询")
	@ApiOperation(value="水蒸汽能耗-通过id查询", notes="水蒸汽能耗-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ConsumptionSteam consumptionSteam = consumptionSteamService.getById(id);
		if(consumptionSteam==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(consumptionSteam);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param consumptionSteam
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ConsumptionSteam consumptionSteam) {
        return super.exportXls(request, consumptionSteam, ConsumptionSteam.class, "水蒸汽能耗");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ConsumptionSteam.class);
    }

}
